const MusicHttp = require("../../utils/MusicHttp");
Page({
  data: {
    banner:[],
    hot:[],
    new:[],
    dj:[],
    currentCity:""
  },
   onShow: function () {
    var city = wx.getStorageSync('city')
    console.log(city);
    this.setData({
      currentCity:city,
    })
    // 获取地理位置
    wx.getLocation({
      altitude: 'altitude',
      highAccuracyExpireTime: 0,
      isHighAccuracy: true,
      type: 'type',
      success: (res) => {
        console.log(res);
        var {latitude,longitude} =res;
        var key ="5SHBZ-N3D3I-M4IGS-5Y7DV-RQXEQ-SRBNV";
        var url =`https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`;
        wx.request({
          url,
          header: {'content-type':'application/json'},
          method: 'GET',
          dataType: 'json',
          responseType: 'text',
          success: (res)=>{
            var currentCity = res.data.result.address_component.city;
                      var storageCity = wx.getStorageSync('city');
                      console.log(storageCity);
                      if(currentCity!=storageCity){
                       wx.showModal({
                            title: '切换城市',
                            content: '当时城市不是您所在的城市',
                            showCancel: true,
                            cancelText: '取消',
                            cancelColor: '#000000',
                            confirmText: '确定',
                            confirmColor: '#3CC51F',
                            success: (result) => {
                                if(result.confirm){
                                    /* 确定设置为当前地理位置 */
                                    this.setData({
                                        currentCity
                                    })
                                    wx.setStorageSync("city", currentCity);
                                }else{
                                    /* 取消就是缓存中的城市 */
                                    this.setData({
                                        currentCity:storageCity
                                    })
                                }
                            }
                        });
                    }
          }
        })
      },
      
    })
  },
  onLoad: async function (options) {
     wx.setStorageSync('citys', [])
    var bannerRes = await MusicHttp.getBannerMusicHttp();
    // console.log(bannerRes.data.banners);
     var hotRes = await MusicHttp.getHotMusicHttp();
    // console.log(hotRes.data.playlists);
    var newRes = await MusicHttp.getNewMusicHttp();
    // console.log(newRes.data.albums);
    var djRes = await MusicHttp.getDjMusicHttp();
    // console.log(djRes.data.result);
    this.setData({
      banner:bannerRes.data.banners,
      hot:hotRes.data.playlists.slice(0,3),
      new:newRes.data.albums.slice(0,3),
      dj:djRes.data.result.slice(0,3)
    })
  },
  onSearch(e){ 
    var key = e.detail;
    console.log(key);
    wx.navigateTo({
      url: `/pages/search/search?keyword=${key}`
    })
    
  },
  onClickCity(e){
    wx.navigateTo({
      url: '../../pages/city/city',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
 

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})